﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TeamTime.Classes;
using StillVoiceCommon;

namespace TeamTime
{
    public partial class CompanyFormControl : System.Web.UI.UserControl
    {

        public string SaveButtonText { get { return this.btnSave.Text; } set { this.btnSave.Text = value; } }

        public Int32 CompanyID
        {
            get { return Convert.ToInt32(this.txtID.Text); }
            set
            {
                //this.txtID.Text = .ToString();
                LoadDetailsForm(value);
            }
        }
        public string CompanyNameRegistered
        {
            get { return this.txtCompanyNameRegistered.Text; }
        }

        public event System.EventHandler SaveClicked;
        private void CompanyFormControl_SaveClicked(object sender, EventArgs e) { }
        public delegate void SaveClickEventHandler(object sender, EventArgs e);

        public event System.EventHandler LoadDupClicked;
        private void CompanyFormControl_LoadDupClicked(object sender, EventArgs e) { }
        public delegate void LoadDupClickEventHandler(object sender, EventArgs e);

        public event System.EventHandler CompanyTypeChanged;
        private void CompanyFormControl_CompanyTypeChanged(object sender, EventArgs e) { }
        public delegate void CompanyTypeChangeEventHandler(object sender, EventArgs e);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Lookups lookups = new Lookups();
                Functions.dropDownListPopulate(this.ddlProvinceID, lookups.getProvinces(false), "ID", "Description");
                EnableControls();
            }
        }
        public void EnableControls()
        {
            Functions.enableChildControls(this.pnlDetails);
            this.txtUserNameModifiedBy.Enabled = false;
            this.txtRecordDate.Enabled = false;
            this.trID.Visible = (txtID.Text!="");
            this.txtID.Enabled = false;
        }
        public void setForAdding()
        {
            Functions.clearChildcontrols(this.pnlDetails);
            this.hypContactRecs.Visible = false;
        }
        private void LoadDetailsForm(Int32 ID)
        {
            Company company = new Company();
            company.load(ID);

            Functions.populateFormControlsFromDataTableFields(company, this.pnlDetails);

            this.txtUserNameModifiedBy.Text = company.UserNameModifiedBy;
            this.CompanyRegNumControl1.CompanyRegNum = company.Fields["CompanyRegNum"].ToString();
            this.CompanyRegNumControl1.CompanyTypeID = Convert.ToInt32(company.Fields["CompanyTypeID"]);



            if (this.txtID.Text == "")
            {
                this.hypEnterprise.Visible = false;
                this.hypSupplier.Visible = false;
            }
            else
            {
                this.hypEnterprise.Visible = true;
                this.hypSupplier.Visible = true;
                if (company.EnterpriseID == null)
                {
                    this.hypEnterprise.Text = "Add as enterprise";
                    this.hypEnterprise.NavigateUrl = "EnterpriseForm.aspx?CompanyID=" + this.txtID.Text;
                }
                else
                {
                    this.hypEnterprise.Text = "Enterprise details";
                    this.hypEnterprise.NavigateUrl = "EnterpriseForm.aspx?ID=" + company.EnterpriseID.ToString();
                }
                if (company.SupplierID == null)
                {
                    this.hypSupplier.Text = "Add as supplier";
                    this.hypSupplier.NavigateUrl = "SuppliersForm.aspx?CompanyID=" + this.txtID.Text;
                }
                else
                {
                    this.hypSupplier.Text = "Supplier details";
                    this.hypSupplier.NavigateUrl = "SuppliersForm.aspx?ID=" + company.SupplierID.ToString();
                }
                this.hypContactRecs.Visible = true;
                this.hypContactRecs.NavigateUrl="ContactRecordsForm.aspx?CompanyID=" + this.txtID.Text;
            }

        }

        protected void lnkLoadDup_Click(object sender, EventArgs e)
        {
            if (this.lnkLoadDup.Attributes["DupID"] != "")
            {
                LoadDetailsForm(Convert.ToInt32(this.lnkLoadDup.Attributes["DupID"]));
                this.lnkLoadDup.Visible = false;
            }

            if (this.LoadDupClicked != null)
                this.LoadDupClicked(sender, e);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            this.lnkLoadDup.Attributes["DupID"] = "";
            this.lnkLoadDup.Visible = false;
            Companies companies = new Companies();
            int dupID = companies.getDuplicateCompanyID(this.txtCompanyName.Text, this.txtID.Text);
            if (dupID != 0)//Duplicate exists
            {
                this.lnkLoadDup.Attributes["DupID"] = dupID.ToString();
                this.lnkLoadDup.Visible = true;
            }
            else //Save
            {
                Company company = new Company();
                if (this.txtID.Text == "") { company.setForAdd(); } else { company.load(this.txtID.Text); }
                company.Fields["Notes"] = this.txtNotes.Text;
                company.Fields["CompanyNameRegistered"] = this.txtCompanyNameRegistered.Text;
                company.Fields["CompanyName"] = this.txtCompanyName.Text;
                company.Fields["TelephoneNumber"] = this.txtTelephoneNumber.Text;
                company.Fields["FaxNumber"] = this.txtFaxNumber.Text;
                company.Fields["ContactPerson"] = this.txtContactPerson.Text;
                company.Fields["AddressPostal"] = this.txtAddressPostal.Text;
                company.Fields["AddressPhysical"] = this.txtAddressPhysical.Text;
                company.Fields["EmailAddress"] = this.txtEmailAddress.Text;
                company.Fields["Notes"] = this.txtNotes.Text;
                company.Fields["VATRegNum"] = this.txtVATRegNum.Text;
                company.Fields["ProvinceID"] = this.ddlProvinceID.SelectedValue;
                company.Fields["WebsiteAddress"] = this.txtWebsiteAddress.Text;
                company.Fields["CityTown"] = this.txtCityTown.Text;
                company.Fields["Suburb"] = this.txtSuburb.Text;
                company.Fields["AddressPostalCode"] = this.txtAddressPostalCode.Text;
                company.Fields["AddressPhysicalCode"] = this.txtAddressPhysicalCode.Text;
                company.Fields["Cellphone"] = this.txtCellphone.Text;
                company.Fields["CompanyRegNum"] = this.CompanyRegNumControl1.CompanyRegNum;
                company.Fields["CompanyTypeID"] = this.CompanyRegNumControl1.CompanyTypeID;
                company.UserIDModifiedBy = Convert.ToInt32(Session["UserID"]);
                company.updateWithUserIDDate();
                this.txtID.Text = company.KeyValue;
                this.SaveMessage.ShowStatusMessage("Successfully saved", 5);
                company = null;

                if (this.SaveClicked != null)
                    this.SaveClicked(sender, e);
            }
        }


        protected void CompanyRegNumControl1_CompanyTypeChanged(object sender, EventArgs e)
        {
            if (this.CompanyTypeChanged != null)
                this.CompanyTypeChanged(sender, e);
        }
    }
}